// pages/agent/plugins/feedback/create/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true,
    showContainer: false,
    animationData: '',
    typeIdx: '',
    tagList: [],
    tag: '',
    formData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    options.typeName && (wx.setNavigationBarTitle({
      title: options.typeName,
    }),this.setData({shareAppMessageTitle: options.typeName}))
    this.setData({
      catId: options.id,
      typeName: options.typeName
    })
    this.loadWorkOrderInfo();
  },

  // 获取工单信息
  loadWorkOrderInfo() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that = this;
    var params = {
      point_url: app.server.agentApi.getSuperWorkOrderDetails,
      catId: this.data.catId
    }
    app.server.getConnect(params).then(function (res) {
      that.fadeOut();
      that.setData(res.data);
    })
  },

  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      typeIdx: e.detail.value,
      tagList: this.data.typeData[e.detail.value]['class']
    })
  },

  // 选择问题类别
  bindSelectTag(e) {
    this.setData({
      tag: e.currentTarget.dataset.name
    })
  },

  submitWorkOrder(e){
    if(this.data.typeData.length>0&&this.data.typeIdx===''){
      app.showConfirm('提示', '请选择'+this.data.typeName+'要反馈的工单类型', false);
      return;
    }
    var data = {
      point_url: app.server.agentApi.submitSuperWorkOrder,
      catId: this.data.catId,
      typeId: this.data.typeData.length>0?this.data.typeData[this.data.typeIdx]['id']:'',
      tag: this.data.tag || '',
      feedback: e.detail
    };
    app.server.getConnect(data).then(function (res) {
      app.showConfirm('提示', '工单提交成功', false, function () {
        wx.navigateBack();
      })
    })
  },

  // 逐渐消失
  fadeOut: function () {
    var that = this;
    var animation = wx.createAnimation({
      delay: 200,
      duration: 600,
      timingFunction: 'ease-in'
    })
    this.animation = animation
    animation.opacity(0).step()
    this.setData({
      animationData: animation.export(),
      showContainer: true
    })
    setTimeout(function () {
      that.setData({
        showLoading: false
      })
    }, 800);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    this.loadWorkOrderInfo();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})